/*
 * Created on Feb 13, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.engine;

import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;

import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.ProfileListenerBase;
import edu.mit.simile.longwell.query.IRefiner;
import edu.mit.simile.longwell.query.QueryException;
import edu.mit.simile.longwell.query.project.IProjection;
import edu.mit.simile.longwell.query.project.IProjector;

/**
 * @author dfhuynh
 */
class Refiner extends ProfileListenerBase implements IRefiner {
	final protected Profile		m_profile;
	final protected QueryEngine	m_engine;
	
	Refiner(Profile profile, QueryEngine engine) {
		m_profile = profile;
		m_engine = engine;
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.IRefiner#refine(java.util.SortedSet, java.lang.String, boolean)
	 */
	public Set refine(
			SortedSet restrictors, String locale, boolean fresh) 
			throws QueryException {
		
		long t = System.currentTimeMillis();
		
		Set objects = null;
		
		Iterator i = restrictors.iterator();
		while (i.hasNext()) {
			Restrictor restrictor = (Restrictor) i.next();
			IProjection projection = null;
			
			if (objects == null) {
				projection = restrictor.m_projector.project();
			} else {
				projection = 
					((IProjector) restrictor.m_projector).project(objects);
			}
			
			objects = restrictor.m_bucketer.getBucket(
				projection, restrictor.m_bucketerParameter);
		}
		
		return objects;
	}

}
